package com.xbdun.service.bmp.biz.impl;

import java.util.concurrent.TimeUnit;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import com.xbdun.common.exceptions.BizException;
import com.xbdun.dao.bmp.mapper.OrgUserMapper;
import com.xbdun.service.bmp.biz.OrgUserBiz;

@Service
public class OrgUserBizImpl implements OrgUserBiz{
	
	@Autowired
	OrgUserMapper orgUserMapper;
	@Autowired
	StringRedisTemplate stringRedisTemplate;
	
	public long getXbdunOrgCode(){
		Long xbdunOrgCode = null;//平台编号
		String string = stringRedisTemplate.opsForValue().get(XBDUN_ORG_CODE+":orgCode");
		if(StringUtils.isBlank(string)){
			xbdunOrgCode = orgUserMapper.getXbdunOrgCode();
			if(null!=xbdunOrgCode&&xbdunOrgCode.longValue()>0){
				stringRedisTemplate.opsForValue().set(XBDUN_ORG_CODE+":orgCode", String.valueOf(xbdunOrgCode), 30,TimeUnit.MINUTES);
			}else{
				throw BizException.DB_SELECTONE_IS_NULL.newInstance("系统未定义平台机构，请联系管理平台");
			}
		}
		return (null==xbdunOrgCode?Long.valueOf(string):xbdunOrgCode.longValue());
	}

	public long cachePutXbdunOrgCode()throws BizException{
		Long xbdunOrgCode = orgUserMapper.getXbdunOrgCode();
		if(null!=xbdunOrgCode&&xbdunOrgCode.longValue()>0){
			stringRedisTemplate.opsForValue().set(XBDUN_ORG_CODE+":orgCode", String.valueOf(xbdunOrgCode), 30,TimeUnit.MINUTES);
		}else{
			throw BizException.DB_SELECTONE_IS_NULL.newInstance("获取平台机构失败");
		}
		return xbdunOrgCode;
	}
}